<?php
namespace app\admin\controller;
class Qyxcx extends Base {
    //列表
    public function lists() {
        if (is_post()) {
            $d = $this->post;
            $map = [];
            if ($d['keyword'] != '') {
                $map[] = ['and', 'title|sub_title|address', 'like', "%{$d['keyword']}%"];
            }
            if ($d['status'] != '') {
                $map[] = ['and', 'status', '=', $d['status']];
            }
            $w = where($map);
            $rs = $this->db->query("select * from qyxcx $w[0] order by id desc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                foreach ($rs[0] as $key => $val) {
                    $rs[0][$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
                }
                suc($rs[0], $rs[1]);
            } else {
                suc([], 0);
            }
        }
        return $this->fetch();
    }

    //添加
    public function add() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
              'title'=>'require',
              'sub_title'=>'require',
              'address'=>'require',
              'lat'=>'require',
              'long'=>'require',
              'phone'=>'require|mobile',
              'pic'=>'require',
              'pics'=>'require',
              'about'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            unset($d['file']);
            $d['create_time'] = time();
            $d['order_no'] = com_order_no();
            $id = $this->db->insert('qyxcx', $d);
            if ($id) {
                m('Attachment','xcom')->imgSure('qyxcx',$id,$d);
                cookie('js_msg', '添加成功');
                cookie('js_function', 'preload');
                return suc();
            } else {
                err('添加失败');
            }
        }
        $this->fetch();
    }

    //编辑
    public function edit() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
              'title'=>'require',
              'sub_title'=>'require',
              'address'=>'require',
              'lat'=>'require',
              'long'=>'require',
              'phone'=>'require|mobile',
              'pic'=>'require',
              'pics'=>'require',
              'about'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            unset($d['file']);
            $d['update_time'] = time();
            $rs = $this->db->update('qyxcx', $d);
            if ($rs) {
                m('Attachment','xcom')->imgSure('qyxcx',$d['id'],$d);
                cookie('js_msg', '修改成功');
                cookie('js_function', 'preload');
                return suc();
            } else {
                err('修改失败');
            }
        }
        $id = $this->get['id'];
        $info = $this->db->query("select * from qyxcx where id=?", [$id], 1);
        $info['pics']    = explode(",",$info["pics"]);
        return $this->fetch(['info' => $info]);
    }

    //删除
    public function del() {
        if (is_post()) {
            $d = $this->post;
            $rs = $this->db->query("select id from tie_cat where parent_id = ? limit 1", [$d['id']], 1);
            if ($rs)
                err('该分类下有子分类,不能被删除!');

            $res = $this->db->query("select id from article where cat_id = ? limit 1", [$d['id']], 1);
            if ($res)
                err('分类下有文章，不能删除!');

            $this->db->delete('tie_cat', $d['id']);
            cookie('js_msg', '删除成功');
            cookie('js_function', 'close|reload');
            return suc();
        }
    }

    #批量删除

    function delall() {
        if (is_post()) {
            $d = $this->post;
            $ids = $d['id'];
            foreach ($ids as $id) {
                $rs = $this->db->query("select id from tie_cat where parent_id = ? limit 1", [$id], 1);
                if ($rs)
                    err('该分类下有子分类,不能被删除!');

                $res = $this->db->query("select id from article where cat_id = ? limit 1", [$id], 1);
                if ($res)
                    err('分类下有帖子，不能删除!');

                $this->db->delete('tie_cat', $id);
            }
            cookie('js_msg', '删除成功');
            cookie('js_function', 'close|reload');
            return suc();
        }
    }

    //详情
    public function detail($id) {
        return $this->fetch();
    }

}
